//
//  KANewMobilePwdVC.swift
//  kano
//
//  Created by Frank on 2024/11/1.
//

import UIKit
import RxCocoa
import RxSwift
import JXSegmentedView

class KANewMobilePwdVC: KABaseVC {
    var name: String = ""
    var identy: String = ""
    var bankcardNo: String = ""
    var codeToken: String = ""
    var newMobile: String = ""
    var buttonHandle: (() -> ())?
    
    private let disposeBag = DisposeBag()

    lazy var var_cornerView: UIView = {
        let var_cornerView = UIView()
        var_cornerView.backgroundColor = UIColor.white
        var_cornerView.layer.cornerRadius = kscale(30)
        var_cornerView.layer.masksToBounds = true
        var_cornerView.frame = CGRectMake(kscale(20), kscale(20), var_screenWidth-kscale(40), kscale(330))
        return var_cornerView
    }()
    
    lazy var var_titleLabel: UILabel = {
        let var_titleLabel = UILabel()
        var_titleLabel.x = kscale(20)
        var_titleLabel.y = kscale(32)
        var_titleLabel.width = var_cornerView.width-kscale(40)
        var_titleLabel.height = kscale(32)
        var_titleLabel.textAlignment = .center
        var_titleLabel.font = UIFont.systemFont(ofSize: kscale(24), weight: .regular)
        var_titleLabel.textColor = .black
        var_titleLabel.text = "Set New Login Password"
        var_titleLabel.sizeToFit()
        return var_titleLabel
    }()
    
    lazy var var_pwdView: KAPwdView = {
        var_pwdView = KAPwdView(placeHolder: "Please enter 6 digits or letters")
        var_pwdView.x = kscale(20)
        var_pwdView.y = kscale(95)
        var_pwdView.size = CGSizeMake(var_cornerView.width-kscale(40), kscale(48))
        return var_pwdView
    }()
    
    lazy var var_confirmPwdView: KAPwdView = {
        var_confirmPwdView = KAPwdView(placeHolder: "Confirm Password")
        var_confirmPwdView.x = kscale(20)
        var_confirmPwdView.y = var_pwdView.bottom+kscale(16)
        var_confirmPwdView.size = CGSizeMake(var_cornerView.width-kscale(40), kscale(48))
        return var_confirmPwdView
    }()
    
    lazy var var_signBtn: UIButton = {
        let var_signBtn = UIButton(frame: CGRectMake(kscale(48), var_confirmPwdView.bottom+kscale(32), var_cornerView.width-kscale(96), kscale(50.0)))
        var_signBtn.setTitle("Confirm", for: .normal)
        var_signBtn.setTitleColor(.white, for: .normal)
        var_signBtn.titleLabel?.font = UIFont.systemFont(ofSize: kscale(17), weight: .bold)
        var_signBtn.backgroundColor = .colorBDBDF5
        var_signBtn.layer.cornerRadius = kscale(25)
        var_signBtn.layer.masksToBounds = true
        var_signBtn.addTarget(self, action: #selector(ka_signButtonClick), for: .touchUpInside)
        return var_signBtn
    }()
}

extension KANewMobilePwdVC {
    override func viewDidLoad() {
        super.viewDidLoad()
        ka_setUI()
        ka_bindUI()
    }
    
    func ka_setUI() {
        view.backgroundColor = .colorf2f2f5
        
        view.addSubview(var_cornerView)
        var_cornerView.addSubview(var_titleLabel)
        var_cornerView.addSubview(var_pwdView)
        var_cornerView.addSubview(var_confirmPwdView)
        var_cornerView.addSubview(var_signBtn)
    }
    
    func ka_bindUI() {
        var_pwdView.contentTF.rx.text
            .orEmpty
            .map { text in
                return (text.count == 4 || text.count == 6) ? .themeColor : .colorBDBDF5
            }
            .bind(to: var_signBtn.rx.backgroundColor)
            .disposed(by: disposeBag)
    }
    
    @objc func ka_signButtonClick() {
        guard var_pwdView.contentTF.text!.verifyPwd else {
            view.makeToast("Please enter 6 digits or letters"); return
        }
        guard var_confirmPwdView.contentTF.text!.verifyPwd else {
            view.makeToast("Please enter 6 digits or letters"); return
        }
        if var_confirmPwdView.contentTF.text != var_pwdView.contentTF.text {
            view.makeToast("The two passwords are inconsistent")
            return
        }
        ka_modifyMobile()
    }
    
    @objc func ka_modifyMobile() {
        let mobile = KAUserManager.shared.user.mobile
        let paramDic: [String: Any] = [
            "userGid": KAUserManager.shared.user.userGid,
            "mobile": newMobile,
            "bvn": identy,
            "cardNo": bankcardNo,
            "newPassword": var_pwdView.contentTF.text!,
            "confirmPassword": var_confirmPwdView.contentTF.text!,
            "verifyCode": codeToken,
            "name": name
        ]
        view.showLoading()
        loginProvider.request(.modifyMobile(paramDic)) { [self] result in
            view.hideLoading()
            switch result {
            case .success(let response):
                if response.statusCode == 200 {
                    NetResponse<KAContent>.handleResponseData(response.data) { isSuccess, message, response in
                        
                        if isSuccess {
                            guard let content = response?.content else {
                                view.makeToast("Verification failure"); return
                            }
                            if content.status == 0 {
                                view.makeToast(message)
                            }  else {
                                var defaultUser = KAUserManager.shared.user
                                defaultUser.mobile = newMobile
                                KAUserManager.shared.user = defaultUser
                                navigationController?.pushViewController(KAChangeMobileResultVC(), animated: true)
                            }
                        } else {
                            self.view.makeToast(message)
//                            //注册失败
//                            KAAFTool.logEvent(event: "SKP_registered_failed")

                        }
                    }
                }else {
                    self.view.makeToast(badNetWorking)
//                    //注册失败
//                    KAAFTool.logEvent(event: "SKP_registered_failed")

                }
            case .failure(let error):
                print(error.localizedDescription)
//                //注册失败
//                KAAFTool.logEvent(event: "SKP_registered_failed")
            }
        }
    }
}


extension KANewMobilePwdVC: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        view.backgroundColor = .colorf2f2f5
        return view
    }
}



